#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct Time {
    year: isize,
    month: isize,
}
impl From<&str> for Time {
    fn from(value: &str) -> Self {
        let mut it = value.split('-');
        let year: isize = it.next().unwrap().parse().unwrap();
        let month: isize = it.next().unwrap().parse().unwrap();
        Self { year, month }
    }
}
impl Time {
    fn go(&mut self) {
        if self.month == 12 {
            self.year += 1;
            self.month = 1;
        } else {
            self.month += 1;
        }
    }
}

const INCREASE: Time = Time {
    year: 2025,
    month: 1,
};

pub fn retire_time(time: &str, tp: &str) -> String {
    let time = Time::from(time);
    let (start_age, end_age, increase_step, increase_gap) = if tp.contains("男") {
        (60 * 12, 63 * 12, 1, 4)
    } else if tp.contains("55") {
        (55 * 12, 58 * 12, 1, 4)
    } else {
        (50 * 12, 55 * 12, 1, 2)
    };
    work(time, start_age, end_age, increase_step, increase_gap)
}

/// calculate the answer when a person is born at `time`, initial retire age is `start_age` months,
/// maximum retire age is `end_age` months, retire age will start increasing from `INCREASE`,
/// every `increase_gap` months the retire age will increase by `increase_step` months.
fn work(
    mut time: Time,
    start_age: isize,
    end_age: isize,
    increase_step: isize,
    increase_gap: isize,
) -> String {
    let mut age = 0;
    let mut retire = start_age;
    if time > INCREASE {
        retire = end_age;
    }
    let mut start_increase = false;
    let mut end_increase = false;
    let mut current = 0;
    // calculate every month
    loop {
        time.go();
        age += 1;
        if time == INCREASE {
            start_increase = true;
        }
        if start_increase && !end_increase {
            if current == 0 {
                retire += increase_step;
            }
            current = (current + 1) % increase_gap;
            if age == start_age || retire == end_age {
                end_increase = true;
            }
        }

        if retire <= age {
            // retire
            return if age % 12 == 0 {
                format!(
                    "{}-{:02},{},{}",
                    time.year,
                    time.month,
                    age as f64 / 12.0,
                    retire - start_age
                )
            } else {
                format!(
                    "{}-{:02},{:.2},{}",
                    time.year,
                    time.month,
                    age as f64 / 12.0,
                    retire - start_age
                )
            };
        }
    }
}
